<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>验证码测试</span>
        </div>
      </template>
      
      <el-tabs v-model="activeTab">
        <el-tab-pane label="短信验证码" name="sms">
          <el-form ref="smsFormRef" :model="smsForm" :rules="smsRules" label-width="100px">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input 
                v-model="smsForm.phonenumber" 
                placeholder="请输入手机号码"
                maxlength="11"
                show-word-limit
                clearable
              >
                <template #append>
                  <el-button 
                    type="primary" 
                    :loading="smsLoading" 
                    :disabled="!smsForm.phonenumber || smsLoading"
                    @click="handleSendSmsCode"
                  >
                    {{ smsLoading ? '发送中...' : '获取验证码' }}
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-form>

          <div class="result-wrapper" v-if="smsResult">
            <el-alert
              :title="smsResult.success ? '发送成功' : '发送失败'"
              :type="smsResult.success ? 'success' : 'error'"
              :description="smsResult.message"
              show-icon
              :closable="false"
            />
          </div>
        </el-tab-pane>

        <el-tab-pane label="邮箱验证码" name="email">
          <el-form ref="emailFormRef" :model="emailForm" :rules="emailRules" label-width="100px">
            <el-form-item label="邮箱地址" prop="email">
              <el-input 
                v-model="emailForm.email" 
                placeholder="请输入邮箱地址"
                clearable
              >
                <template #append>
                  <el-button 
                    type="primary" 
                    :loading="emailLoading" 
                    :disabled="!emailForm.email || emailLoading"
                    @click="handleSendEmailCode"
                  >
                    {{ emailLoading ? '发送中...' : '获取验证码' }}
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-form>

          <div class="result-wrapper" v-if="emailResult">
            <el-alert
              :title="emailResult.success ? '发送成功' : '发送失败'"
              :type="emailResult.success ? 'success' : 'error'"
              :description="emailResult.message"
              show-icon
              :closable="false"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import request from '@/utils/request';

const activeTab = ref('sms');

// 短信验证码相关
const smsFormRef = ref<FormInstance>();
const smsLoading = ref(false);
const smsResult = ref<{ success: boolean; message: string } | null>(null);

const smsForm = reactive({
  phonenumber: ''
});

const smsRules = reactive<FormRules>({
  phonenumber: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
});

// 邮箱验证码相关
const emailFormRef = ref<FormInstance>();
const emailLoading = ref(false);
const emailResult = ref<{ success: boolean; message: string } | null>(null);

const emailForm = reactive({
  email: ''
});

const emailRules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
});

/** 发送短信验证码 */
const handleSendSmsCode = async () => {
  if (!smsFormRef.value) return;
  
  await smsFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      smsLoading.value = true;
      smsResult.value = null;
      
      try {
        const res = await request({
          url: '/resource/sms/code',
          method: 'get',
          params: { phonenumber: smsForm.phonenumber }
        });
        
        if (res.code === 200) {
          smsResult.value = {
            success: true,
            message: '验证码已发送，请注意查收'
          };
          ElMessage.success('验证码发送成功');
        } else {
          smsResult.value = {
            success: false,
            message: res.msg || '发送失败'
          };
          ElMessage.error(res.msg || '发送失败');
        }
      } catch (error: any) {
        smsResult.value = {
          success: false,
          message: error.message || '发送失败'
        };
        ElMessage.error(error.message || '发送失败');
      } finally {
        smsLoading.value = false;
      }
    }
  });
};

/** 发送邮箱验证码 */
const handleSendEmailCode = async () => {
  if (!emailFormRef.value) return;
  
  await emailFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      emailLoading.value = true;
      emailResult.value = null;
      
      try {
        const res = await request({
          url: '/resource/email/code',
          method: 'get',
          params: { email: emailForm.email }
        });
        
        if (res.code === 200) {
          emailResult.value = {
            success: true,
            message: '验证码已发送，请注意查收邮件'
          };
          ElMessage.success('验证码发送成功');
        } else {
          emailResult.value = {
            success: false,
            message: res.msg || '发送失败'
          };
          ElMessage.error(res.msg || '发送失败');
        }
      } catch (error: any) {
        emailResult.value = {
          success: false,
          message: error.message || '发送失败'
        };
        ElMessage.error(error.message || '发送失败');
      } finally {
        emailLoading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.box-card {
  max-width: 600px;
  margin: 20px auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-wrapper {
  margin-top: 20px;
}

:deep(.el-input-group__append) {
  padding: 0;
}

:deep(.el-input-group__append .el-button) {
  margin: 0;
  border: none;
  border-radius: 0;
  height: 100%;
  padding: 0 20px;
}

:deep(.el-alert) {
  margin-top: 20px;
}
</style> 